package com.huaxin.hxenum.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.huaxin.hxenum.base.BaseCodeEnum;
import com.huaxin.hxenum.base.Colorful;
import com.huaxin.hxenum.base.Describable;

/**
 * 防火等级
 */
public enum FireRating implements BaseCodeEnum<Integer>, Describable, Colorful {

    ONE(1, "一级", "耐火极限 ≥ 3 小时", "#00A854"),     // 绿色 - 安全
    TWO(2, "二级", "耐火极限 ≥ 2 小时", "#108EE9"),     // 蓝色 - 较安全
    THREE(3, "三级", "耐火极限 ≥ 1 小时", "#FFBF00"),   // 橙色 - 注意
    FOUR(4, "四级", "耐火极限 < 1 小时", "#F5222D");    // 红色 - 危险

    @EnumValue
    private final Integer code;
    private final String displayName;
    private final String description;
    private final String color;

    FireRating(Integer code, String displayName, String description, String color) {
        this.code = code;
        this.displayName = displayName;
        this.description = description;
        this.color = color;
    }

    @JsonValue
    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getColor() {
        return color;
    }

    /**
     * 根据 code 获取枚举实例
     */
    public static FireRating of(int code) {
        for (FireRating level : values()) {
            if (level.code == code) {
                return level;
            }
        }
        return null;
    }

    /**
     * 校验 code 是否有效
     */
    public static boolean isValid(int code) {
        return of(code) != null;
    }

    /**
     * 兼容字符串输入（用于校验）
     */
    public static boolean isValid(String codeStr) {
        if (codeStr == null || codeStr.trim().isEmpty()) {
            return false;
        }
        try {
            int code = Integer.parseInt(codeStr.trim());
            return isValid(code);
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
